﻿Imports System.Windows.Forms

Public Class RepositoryManager

    Public repos As New List(Of String)
    Public InstalledScript As String

    Private Sub OK_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OK_Button.Click
        Try
            Dim sels As String = ListBox1.SelectedItem
            Dim _getname1 As String() = sels.Split("/")
            Dim name As String = _getname1(_getname1.Count - 1)
            System.Console.WriteLine(name)
            Dim opfile As String = OFCDirectory & "\Scripts\" & name
            DownloadFile(sels, opfile)
            System.Console.WriteLine(opfile)
            InstalledScript = opfile
            Me.DialogResult = System.Windows.Forms.DialogResult.OK
        Catch ex As Exception
            Me.DialogResult = System.Windows.Forms.DialogResult.Cancel
        End Try

        Me.Close()
    End Sub

    Private Sub Cancel_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Cancel_Button.Click
        Me.DialogResult = System.Windows.Forms.DialogResult.Cancel
        Me.Close()
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim repfile As New IO.FileInfo(OFCDirectory & "\Repositories.xml")
        If repfile.Exists = False Then
            Dim _xml = <?xml version="1.0" encoding="utf-8"?>
                       <Repositories>

                       </Repositories>
            Dim sw As New IO.StreamWriter(repfile.FullName)
            sw.Write(_xml.ToString)
            sw.Close()
        End If
        Dim dlg As New UrlDialog
        If dlg.ShowDialog = Forms.DialogResult.OK Then
            Try
                If My.Computer.Network.Ping(New Uri(dlg.SelectedUri)) Then
                    repos.Add(dlg.SelectedUri)
                    Dim data = <?xml version="1.0" encoding="utf-8"?>
                               <Respositories>
                                   <%= From item In repos _
                                       Select <Repository><%= item %></Repository> %>
                               </Respositories>
                    Dim sw As New IO.StreamWriter(OFCDirectory & "\Repositories.xml")
                    sw.Write(data.ToString)
                    sw.Close()
                    ComboBox1.Items.Add(dlg.SelectedUri)
                End If
            Catch ex As Exception

            End Try
        End If
    End Sub

    Private Sub RepositoryManager_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim repfile As New IO.FileInfo(OFCDirectory & "\Repositories.xml")
        If repfile.Exists = True Then
            Dim xDoc As XDocument = XDocument.Load(repfile.FullName)
            Dim data = From item In xDoc...<Repository> _
                       Select item.Value _
                       Order By Value Ascending
            For Each _i In data
                ComboBox1.Items.Add(_i)
            Next
        End If
    End Sub

    Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
        Dim remotefile As String = ComboBox1.SelectedItem
        MsgBox(remotefile)
        If My.Computer.Network.Ping(New Uri(remotefile)) Then
            DownloadFile(remotefile, OFCDirectory & "\_cache1.xml")
            Dim xDoc As XDocument = XDocument.Load(OFCDirectory & "\_cache1.xml")
            Dim data = From item In xDoc...<ScriptsRepository>.<Script> _
                       Select item.<SourceFile>, item.<Author>, item.<Versions>, item.<WebSite> _
                       Order By SourceFile.Value Ascending
            For Each item In data
                ListBox1.Items.Add(item.SourceFile.Value)
            Next
        Else
            MsgBox("Can't find file at " & New Uri(remotefile).AbsolutePath)
        End If
    End Sub

    Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged

        
    End Sub
End Class
